package org.wwb.antbox.user.service.system;

import org.ant.commons.commons.query.enums.Sort;
import org.ant.commons.models.model.BaseIdModel;
import org.ant.commons.models.page.PageInfo;
import org.ant.commons.models.result.BaseEditResult;
import org.ant.commons.models.result.BaseListResult;
import org.ant.commons.models.result.BasePageResult;
import org.ant.commons.models.result.BaseResult;
import org.ant.mybatis.query.Query;
import org.ant.service.common.BaseService;
import org.apache.dubbo.config.annotation.Service;
import org.wwb.antbox.user.data.system.SysSystemMapper;
import org.wwb.antbox.user.iservice.system.ISysSystemService;
import org.wwb.antbox.user.model.system.entity.SysSystem;
import org.wwb.antbox.user.model.system.model.SysSystemAddModel;
import org.wwb.antbox.user.model.system.model.SysSystemDelModel;
import org.wwb.antbox.user.model.system.model.SysSystemEditModel;
import org.wwb.antbox.user.model.system.model.SysSystemPageModel;
import org.wwb.antbox.user.model.system.view.SysSystemPageView;
import java.util.List;

/**
 * @Author Bond
 * @Date: 2019/10/4 20:00
 * @Description: 系统接口实现
 */
@Service(registry = {"registryConfigUser"},provider ="dubboProviderUser")
public class SysSystemServiceImpl extends BaseService<SysSystemMapper, SysSystem, String> implements ISysSystemService {
    /**
     * 系统分页查询
     * @param sysUserPageModel 分页查询参数模型
     * @return BasePageResult<SysSystemPageView>
     */
    @Override
    public BasePageResult<SysSystemPageView> getSysSystemPage(SysSystemPageModel sysUserPageModel) {
        Query build = Query.build(sysUserPageModel)
                .orderby("system_sort", Sort.ASC)
                .page(sysUserPageModel.getPageIndex(),sysUserPageModel.getPageSize());

        PageInfo<SysSystem> sysUserPageInfo = queryPage(build);
        PageInfo<SysSystemPageView> sysUserPageViewPageInfo = new SysSystemPageView().toPageView(sysUserPageInfo);

        BasePageResult<SysSystemPageView> basePageResult = new BasePageResult<>();
        basePageResult.setModel(sysUserPageViewPageInfo);
        basePageResult.setSuccess(true);
        return basePageResult;
    }

    @Override
    public BaseResult<Integer> saveSysSystem(SysSystemAddModel sysUserAddModel) {
        SysSystem sysUser = sysUserAddModel.modelToEntity();
        int save = mapper.save(sysUser);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(save);
        baseResult.setSuccess(save == 1);
        return baseResult;
    }

    @Override
    public BaseResult<Integer> delSysSystem(SysSystemDelModel sysUserDelModel) {
        Query query = new Query();
        query.in("id",sysUserDelModel.getIds());
        int delete = mapper.deleteByQuery(query);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(delete);
        baseResult.setSuccess(delete >= 1);
        return baseResult;
    }

    @Override
    public BaseEditResult doEditSysSystem(BaseIdModel<String> baseIdModel) {
        SysSystem sysUser = mapper.getById(baseIdModel.getId());
        BaseEditResult baseEditResult = new BaseEditResult();
        baseEditResult.setModel(sysUser);
        baseEditResult.setSuccess(true);
        return baseEditResult;
    }

    @Override
    public BaseResult<Integer> editSysSystem(SysSystemEditModel sysUserEditModel) {
        SysSystem sysUser = sysUserEditModel.modelToEntity();
        int edit = mapper.updateIgnoreNull(sysUser);
        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(edit);
        baseResult.setSuccess(edit >= 1);
        return baseResult;
    }

    @Override
    public BaseListResult getSysSystemList() {
        Query query = new Query()
                .orderby("system_sort", Sort.ASC);
        List<SysSystem> sysSystemList = mapper.list(query);

        BaseListResult baseListResult = new BaseListResult();
        baseListResult.setModel(sysSystemList);
        baseListResult.setSuccess(true);
        return baseListResult;
    }
}
